package com.geostar.constructionland.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geostar.constructionland.index.entity.InxStorageAcotEntity;
import com.geostar.constructionland.index.entity.OrgOrganization;
import com.geostar.constructionland.index.entity.JsydDicEntry;
import com.geostar.constructionland.index.entity.model.IndexOrdinatingModel;
import com.geostar.constructionland.index.entity.model.IndexStatisticsModel;
import com.geostar.constructionland.index.mapper.IndexStatisticsMapper;
import com.geostar.constructionland.index.mapper.InxStorageAcotMapper;
import com.geostar.constructionland.index.mapper.JsydDicEntryMapper;
import com.geostar.constructionland.index.mapper.OrgOrganizationMapper;
import com.geostar.constructionland.index.service.IndexService;
import com.geostar.constructionland.index.util.FormFieldsUtils;
import com.geostar.constructionland.index.util.FormatUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.logging.Logger;


/**
 * @author df
 * @since 2020-5-26 10:07
 **/
@Service
public class IndexSerImpl implements IndexService {


    @Resource
    IndexStatisticsMapper statisticsMapper;
    @Resource
    JsydDicEntryMapper jsydDicEntryMapper;

    @Resource
    OrgOrganizationMapper orgOrganizationMapper;

    @Resource
    InxStorageAcotMapper inxStorageAcotMapper;

    public Logger log = Logger.getLogger("IndexSerImpl");

    Calendar cal = Calendar.getInstance();

    @Override
    public List queryIndexStatistics(IndexStatisticsModel statisticsModel) {

        QueryWrapper<IndexStatisticsModel>  queryWrapper=new QueryWrapper<>();
        String  currentXzqhdm = "440000";
        if(StringUtils.isNotBlank(statisticsModel.getUserOrgId())){
            OrgOrganization orgOrganization = orgOrganizationMapper.selectById(statisticsModel.getUserOrgId());
            currentXzqhdm = orgOrganization.getAREA();
        }
        if(StringUtils.isNotBlank(statisticsModel.getZbnf())){
            queryWrapper.eq("zbnf",statisticsModel.getZbnf());
        }else{
            queryWrapper.eq("zbnf",cal.get(Calendar.YEAR));
        }
        if(StringUtils.isNotBlank(statisticsModel.getZblx())){

            queryWrapper.in("zblx",FormatUtils.getStringByList(statisticsModel.getZblx()));
        }
        //如果当前登陆用户是县级用户
        if(StringUtils.isNotBlank(currentXzqhdm) && !"0000".equals(currentXzqhdm.substring(2, currentXzqhdm.length()))
                && !"00".equals(currentXzqhdm.substring(4, currentXzqhdm.length()))){
                //查询父级id
                String parentXzqhdm=parentXzqhdm(currentXzqhdm);
                String xzqhdm = "440000,"+currentXzqhdm+","+parentXzqhdm;
                queryWrapper.in("xzqhdm", FormatUtils.getStringByList(xzqhdm));

        }else{
            if(StringUtils.isNotBlank(statisticsModel.getZbnf())){
                queryWrapper.eq("xzqhdm_province",statisticsModel.getXzqhdmProvince());
            }
            if(StringUtils.isNotBlank(statisticsModel.getXzqhdmCity())){
                queryWrapper.eq("xzqhdm_city",statisticsModel.getXzqhdmCity());
            }
            if(StringUtils.isNotBlank(statisticsModel.getXzqhdmCountry())){
                queryWrapper.eq("xzqhdm_country",statisticsModel.getXzqhdmCountry());
            }

        }

        List<Map<String, String>> zbzzMaps=new ArrayList<>();
        List<Map<String, String>> zbflMaps=new ArrayList<>();
        List<Map<String, String>> ftczbMaps=new ArrayList<>();
        // 如果当前登陆用户是省级用户
        if(StringUtils.isNotBlank(currentXzqhdm) && "0000".equals(currentXzqhdm.substring(2, currentXzqhdm.length()))){
            //根据指标年份查询指标总账信息
            zbzzMaps = statisticsMapper.queryIndexByZbzzWrapper(queryWrapper);

            //根据指标年份查询指标分类信息
            zbflMaps = statisticsMapper.queryIndexByFlzbzzWrapper(queryWrapper);

//            查询非统筹使用指标信息
            ftczbMaps=statisticsMapper.queryIndexFtcsyWrapper(queryWrapper);

        }else{// 如果当前登陆用户不是省级用户
            //根据指标年份查询指标总账信息
            zbzzMaps=statisticsMapper.queryIndexByPTZbzzWrapper(queryWrapper);

            //根据指标年份查询指标分类信息
            zbflMaps=statisticsMapper.queryIndexByPTZbFlzzWrapper(queryWrapper);

//            查询非统筹使用指标信息
            ftczbMaps=statisticsMapper.queryIndexOtherFtcsyWrapper(queryWrapper);

        }

        List resultList = queryZbzzContact(zbzzMaps, zbflMaps, ftczbMaps, currentXzqhdm);

        log.info("指标年份查询指标总账信息数据集合");
        log.info(resultList.toString());




        return resultList;
    }

    @Override
    public  Page<InxStorageAcotEntity> queryIndexOrdinating(IndexOrdinatingModel indexOrdinatingModel) {
        Page<InxStorageAcotEntity> page = new Page<>(indexOrdinatingModel.getPagenum(),indexOrdinatingModel.getPagesize());
        QueryWrapper<InxStorageAcotEntity> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotBlank(indexOrdinatingModel.getXzqhdmCountry())){

            if(StringUtils.isNotBlank(indexOrdinatingModel.getXzqhdmCity())){
                queryWrapper.and(wrapper -> wrapper.eq("xzqhdm_city",indexOrdinatingModel.getXzqhdmCity()).or().like("xzqhdm_country",indexOrdinatingModel.getXzqhdmCountry()));
            }else{
                queryWrapper.like("xzqhdm_country",indexOrdinatingModel.getXzqhdmCountry());
            }
        }
        if(StringUtils.isNotBlank(indexOrdinatingModel.getXzqhdmProvince())){
            queryWrapper.eq("xzqhdm_province",indexOrdinatingModel.getXzqhdmProvince());
        }
       if(StringUtils.isNotBlank(indexOrdinatingModel.getZbnf())){
            queryWrapper.eq("zbnf",indexOrdinatingModel.getZbnf());
        }

        queryWrapper.in("tclx",FormatUtils.getStringByList(indexOrdinatingModel.getTclx()));

        if(StringUtils.isNotBlank(indexOrdinatingModel.getArrZblx())){
            queryWrapper.in("zblx",FormatUtils.getStringByList(indexOrdinatingModel.getArrZblx()));
        }

        if(StringUtils.isNotBlank(indexOrdinatingModel.getSortOrder()) && StringUtils.isNotBlank(indexOrdinatingModel.getSortName())){
            if (indexOrdinatingModel.getSortOrder().equals("desc") ?  true:false ){
                queryWrapper.orderByDesc(indexOrdinatingModel.getSortName());
            }else{

                queryWrapper.orderByAsc(indexOrdinatingModel.getSortName());
            }
        }
        page = inxStorageAcotMapper.selectPage(page,queryWrapper);
        return page;

    }

    @Override
    public void updateIndexOrdinating(String id, String overallStatus, String remark) {
        InxStorageAcotEntity storageAcotEntity = inxStorageAcotMapper.selectById(id);
        if(storageAcotEntity!=null){
                storageAcotEntity.setId(id);
                storageAcotEntity.setOverallStatus(overallStatus);
                storageAcotEntity.setRemark(remark);
            inxStorageAcotMapper.updateById(storageAcotEntity);

        }
    }


    /**
     * 将查询出来的两个数组拼接
     * @param zbzzMaps 指标总账信息
     * @param zbflMaps 指标分类信息
     * @param ftczbMaps 指标非统筹信息
     * @param currentXzqhdm
     * @return
     */
    private List queryZbzzContact(List<Map<String, String>> zbzzMaps,List<Map<String, String>> zbflMaps,List<Map<String, String>> ftczbMaps,String currentXzqhdm){
        List resultList=new ArrayList();
        // 循环指标总账
        for(Map<String, String> zbzz:zbzzMaps){

            Map<String, String> result=new HashMap<>();
//            ZBLX, ZBNF, XZQHDM, XZQHMC, XZQHDM_PROVINCE, XZQHDM_CITY, XZQHDM_COUNTRY, XZJSYD, NYD, GD
            result.put("zblx",zbzz.get("ZBLX"));
            result.put("zbnf",zbzz.get("ZBNF"));
            result.put("xzqhdm",zbzz.get("XZQHDM"));
            result.put("xzqhmc",zbzz.get("XZQHMC"));
            result.put("xzqhdmProvince",zbzz.get("XZQHDM_PROVINCE"));
            result.put("xzqhdmCity",zbzz.get("XZQHDM_CITY"));
            result.put("xzqhdmCountry",zbzz.get("XZQHDM_COUNTRY"));
            result.put("xzjsyd",zbzz.get("XZJSYD"));
            result.put("nyd",zbzz.get("NYD"));
            result.put("gd",zbzz.get("GD"));

            // 循环指标分类信息
            for(Map<String, String> zbfl:zbflMaps){
                // ZBLX, ZBNF, XZQHDM, XZQHMC, CKZT, XZQHDM_PROVINCE, XZQHDM_CITY, XZQHDM_COUNTRY, XZJSYD, NYD, GD
                String ptZbFlzzXzqhdm = "";
                String ptZbzzXzqhdm = "";
                // 如果当前登录用户是省级用户
                if(StringUtils.isNotBlank(currentXzqhdm) && "0000".equals(currentXzqhdm.substring(2, currentXzqhdm.length()))){
                    ptZbFlzzXzqhdm = StringUtils.isNotBlank(zbfl.get("XZQHDM_CITY")) ? zbfl.get("XZQHDM_CITY") : "440000";
                    ptZbzzXzqhdm = StringUtils.isNotBlank(zbzz.get("XZQHDM_CITY"))?zbzz.get("XZQHDM_CITY") : "440000";
                }
                // 如果当前登录用户是其他用户
                if(StringUtils.isBlank(ptZbFlzzXzqhdm)){
                    if(StringUtils.isNotBlank(zbfl.get("XZQHDM"))){
                        ptZbFlzzXzqhdm = zbfl.get("XZQHDM");
                        ptZbzzXzqhdm = zbzz.get("XZQHDM");
                        if(StringUtils.isBlank(ptZbFlzzXzqhdm) && StringUtils.isNotBlank(ptZbzzXzqhdm)){
                            ptZbFlzzXzqhdm = ptZbzzXzqhdm;
                            ptZbzzXzqhdm = zbzz.get("XZQHDM");
                        }else if(StringUtils.isBlank(ptZbzzXzqhdm)){
                            ptZbFlzzXzqhdm = zbfl.get("XZQHDM");
                            ptZbzzXzqhdm = ptZbFlzzXzqhdm;
                        }
                    }else{
                        ptZbFlzzXzqhdm = StringUtils.isNotBlank(zbfl.get("XZQHDM_CITY")) ? zbfl.get("XZQHDM_CITY") : "440000";
                        ptZbzzXzqhdm = StringUtils.isNotBlank(zbzz.get("XZQHDM_CITY"))?zbzz.get("XZQHDM_CITY") : "440000";
                    }
                }
                // 判断是否有指标操作信息
                if (ptZbFlzzXzqhdm.equals(ptZbzzXzqhdm) && zbfl.get("ZBLX").equals(zbzz.get("ZBLX"))) {
                    // 设置预扣指标信息
                    if ("01".equals(zbfl.get("CKZT"))) {
                        // 建设用地预扣指标信息
                        result.put("xzjsyd_01",zbfl.get("XZJSYD"));
                        // 农用地预扣指标信息
                        result.put("nyd_01",zbfl.get("NYD"));
                        // 耕地预扣指标信息
                        result.put("gd_01",zbfl.get("GD"));
                        // 设置使用指标信息
                    }else if("02".equals(zbfl.get("CKZT"))){
                        // 建设用地使用指标信息
                        result.put("xzjsyd_02",zbfl.get("XZJSYD"));
                        // 农用地使用指标信息
                        result.put("nyd_02",zbfl.get("NYD"));
                        // 耕地使用指标信息
                        result.put("gd_02",zbfl.get("GD"));
                        // 设置冻结指标信息
                    }else if("08".equals(zbfl.get("CKZT"))){
                        // 建设用地冻结指标信息
                        result.put("xzjsyd_08",zbfl.get("XZJSYD"));
                        // 农用地冻结指标信息
                        result.put("nyd_08",zbfl.get("NYD"));
                        // 耕地冻结指标信息
                        result.put("gd_08",zbfl.get("GD"));
                    }


                }
            }

            //拼接非统筹使用指标
            for(Map<String, String> ftczb:ftczbMaps){
                //XZQHDM, XZQHMC, ZBLX, ZBNF, XZQHDM_PROVINCE, XZQHDM_COUNTRY, XZQHDM_CITY, XZJSYD, NYD, GD
                String ftcsyXzqhdm = "";
                String ptZbzzXzqhdm = "";
                // 如果当前登录用户是省级用户
                if(StringUtils.isNotBlank(currentXzqhdm) && "0000".equals(currentXzqhdm.substring(2, currentXzqhdm.length()))){
                    ftcsyXzqhdm = StringUtils.isNotBlank(ftczb.get("XZQHDM_CITY")) ? ftczb.get("XZQHDM_CITY") : "440000";
                    ptZbzzXzqhdm = StringUtils.isNotBlank(zbzz.get("XZQHDM_CITY"))?zbzz.get("XZQHDM_CITY") : "440000";
                }
                // 如果当前登录用户是其他用户
                if(StringUtils.isBlank(ftcsyXzqhdm)){
                    if(StringUtils.isNotBlank(ftczb.get("XZQHDM"))){
                        ftcsyXzqhdm = ftczb.get("XZQHDM");
                        ptZbzzXzqhdm = zbzz.get("XZQHDM");
                        if(StringUtils.isBlank(ftcsyXzqhdm) && StringUtils.isNotBlank(ptZbzzXzqhdm)){
                            ftcsyXzqhdm = ptZbzzXzqhdm;
                            ptZbzzXzqhdm = zbzz.get("XZQHDM");
                        }else if(StringUtils.isBlank(ptZbzzXzqhdm)){
                            ftcsyXzqhdm = ftczb.get("XZQHDM");
                            ptZbzzXzqhdm = ftcsyXzqhdm;
                        }
                    }else{
                        ftcsyXzqhdm = StringUtils.isNotBlank(ftczb.get("XZQHDM_CITY")) ? ftczb.get("XZQHDM_CITY") : "440000";
                        ptZbzzXzqhdm = StringUtils.isNotBlank(zbzz.get("XZQHDM_CITY"))?zbzz.get("XZQHDM_CITY") : "440000";
                    }
                }
                if(ftcsyXzqhdm.equals(ptZbzzXzqhdm) && ftczb.get("ZBLX").equals(zbzz.get("ZBLX"))){
                    // 建设用地非统筹指标信息
                    result.put("xzjsyd_ftc",ftczb.get("XZJSYD"));
                    // 农用地非统筹指标信息
                    result.put("nyd_ftc",ftczb.get("NYD"));
                    // 耕地非统筹指标信息
                    result.put("gd_ftc",ftczb.get("GD"));
                }
            }


            resultList.add(result);

        }

        return resultList;
    }
    /**
     * 获取父级行政区划代码
     * @param currentXzqhdm
     * @return
     */
    public String parentXzqhdm(String currentXzqhdm){
        String parentXzqhdm = "";
        if(StringUtils.isNotBlank(currentXzqhdm)){
            QueryWrapper<JsydDicEntry>  queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("DICTTYPEID","XZQHDM");
            queryWrapper.eq("DICTID",currentXzqhdm);
            queryWrapper.orderByAsc("DICTORDER");

            List<JsydDicEntry> jsydDicEntrieList = jsydDicEntryMapper.selectList(queryWrapper);
            if(jsydDicEntrieList.size()>0){
                queryWrapper=new QueryWrapper<>();
                queryWrapper.eq("DICTENTRYID",jsydDicEntrieList.get(0).getPARENTID());
                queryWrapper.orderByAsc("DICTORDER");
                List<JsydDicEntry> jsydDicEntries = jsydDicEntryMapper.selectList(queryWrapper);
                if(jsydDicEntries.size()>0){
                    parentXzqhdm=jsydDicEntries.get(0).getDICTID();
                }

            }

        }
        return parentXzqhdm;
    }



}
